package shapewar.view.tank;

import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import shapewar.conf.Conf;
import shapewar.engine.calc.Pos;
import shapewar.engine.intefac.IHittable;
import shapewar.engine.intefac.IMovable;
import shapewar.engine.model.Protector;
import shapewar.engine.model.Tank;
import shapewar.engine.model.abst.Model;
import shapewar.engine.presenter.EngineThread;
import shapewar.engine.presenter.Presenter;
import shapewar.view.myview.MyView;
import shapewar.view.myview.PaintFilter;

public class ProtectorView extends MyView {
	private Protector ptr = (Protector) getModel();
	float v[][];
	public ProtectorView(Presenter pst,Model ptr) {
		super(pst,ptr);
		paint.setColor(Conf.protector_color);
		paint.setMaskFilter(PaintFilter.getLightFilter());
	}
	
	@Override
	protected void onDraw(Canvas canvas) {
		paint.setAlpha(255);
		
		if(((TankView)(ptr.getFather().getView())).hidingCheck(paint))
			return;
		
		v = ptr.getPtr();
		Pos rpos = getRelativePos();
		for(int i=0;i<ptr.count;i++){
			canvas.drawCircle(rpos.x + v[i][0] * rate, rpos.y + v[i][1] * rate, ptr.R * rate, paint);
		}
	}
	
	public void fadeTo(final IHittable killer) {
		pst.bridge.addView(this);
		new EngineThread(30){
			final int max_t = 30;
			int t = max_t;
			@Override
			public void repeat() {
				if(t-->1){
					ptr._x = killer.getPos().x + (ptr._x - killer.getPos().x)*0.92f;
					ptr._y = killer.getPos().y + (ptr._y - killer.getPos().y)*0.92f;
					paint.setAlpha((int)(1f*t/max_t*120));
					invalidate();
				}else{
					remove();
					destory();
				}
			}
		}.start();
	}
	
	
	public void remove(){
		pst.bridge.removeView(this);
	}
}
